package com.example.myfirstprj;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.util.Log;

public class MainActivity extends FragmentActivity {
    private boolean mPaused = false;
    protected String mCurrentScreenId;

    private FragmentManager mFMan;
    private FragmentTransaction mTrans;

    
    private Handler mHandler;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        
        mHandler = new Handler();
        Log.i("MainActivity","onCreate");
        setContentView(R.layout.activity_main);
        mFMan = getSupportFragmentManager();
        if (savedInstanceState == null) {
        	loadScreenAsRoot(Screen.createInstance(CaptureScreen.class, null));
        }
    }

    @Override
    protected void onStart() {
        super.onStart();
    }

    @Override
    protected void onStop() {
        Log.i("MainActivity", "onStop: ");
        super.onStop();
    }

    protected void onDestroy() {
    	Log.i("MainActivity", "before call onDestroy");
    	super.onDestroy();
    	
    }

   

    @Override
    protected void onPause() {
    	Log.i("MainActivity","onPause");
        mPaused = true;
        super.onPause();
    }


    public boolean isPaused() {
        return mPaused;
    }

    
    
    @Override
    protected void onResume() {
    	Log.i("MainActivity","onResume");
        mPaused = false;
        super.onResume();
    }

    public void loadScreenAsRoot(Screen sc) {
        this.loadScreen(sc
                , 0, 0, 0, 0
                , true, false, true, false
        );
    }

    public void loadScreen(Screen sc) {
        this.loadScreen(sc
                , R.anim.slide_in_right, R.anim.slide_out_left
                , R.anim.slide_in_left, R.anim.slide_out_right
                , false, true, true, false
        );
    }
    public void loadScreen(Screen sc, boolean saveBackstack, boolean clearScreenIfExists) {
        this.loadScreen(sc
                , R.anim.slide_in_right, R.anim.slide_out_left
                , R.anim.slide_in_left, R.anim.slide_out_right
                , false, saveBackstack, true, clearScreenIfExists
        );
    }

    public void loadScreenNoTransition(Screen sc) {
        this.loadScreen(sc
                , R.anim.slide_in_right, R.anim.slide_out_left
                , R.anim.slide_in_left, R.anim.slide_out_right
                , false, true, false, false
        );
    }


    public void loadScreen(Screen sc, boolean saveBackstack) {
        this.loadScreen(sc
                , R.anim.slide_in_right, R.anim.slide_out_left
                , R.anim.slide_in_left, R.anim.slide_out_right
                , false, saveBackstack, true, false
        );
    }


    public void loadScreen(Screen sc, int enterAnim, int exitAnim, int popEnterAnim, int popExitAnim, boolean clearBackstack, boolean saveBackstack, boolean isShowTransition, boolean flagClearScreenIfExists) {
        if (sc == null || mPaused)
            return;
        String scId = sc.getScreenId();
        Fragment old = mFMan.findFragmentByTag(scId);
        
        // dau's 08/05/2014: flagClearScreenIfExists = true => remove old fragment => add new fragment
        if (mTrans == null)
            mTrans = mFMan.beginTransaction();
        
        Log.d("dau's", "flagClearScreenIfExists: " + flagClearScreenIfExists);
        if (old != null && mCurrentScreenId != null && scId.equals(mCurrentScreenId)) {
        	// dau's 08/05/2014
        	Log.d("dau's", "Fragment Old is exsits!!");
        	if (flagClearScreenIfExists) {
        		Log.d("dau's", "Remove fragment old!!!");
        		mTrans.remove(old);
        	} else {
        		// dau's 08/05/2014
        		mTrans = null;
        		return;
        	}
        }

        if (clearBackstack)
            clearBackstack();
        if (isShowTransition)
            mTrans.setCustomAnimations(enterAnim, exitAnim, popEnterAnim, popExitAnim);
        mTrans.replace(R.id.container, sc, scId);

        if (saveBackstack)
            mTrans.addToBackStack(null);

        mTrans.commit();
        mTrans = null;
    }

    public void clearBackstack() {
        final int n = mFMan.getBackStackEntryCount();
        for (int i = n - 1; i >= 0; i--) {
            mFMan.popBackStack(null, FragmentManager.POP_BACK_STACK_INCLUSIVE);
        }
    }

    public String getCurrentScreenId() {
        return mCurrentScreenId;
    }

    public Screen getCurrentScreen() {
        Screen ret = null;
        if (mCurrentScreenId != null) {
            try {
//				mFMan.executePendingTransactions();
                ret = (Screen) mFMan.findFragmentByTag(mCurrentScreenId);

            } catch (ClassCastException e) {
                e.printStackTrace();
            }
        }
        return ret;
    }

    public void goBackScreen(int steps) {
        if (mFMan != null && mFMan.getBackStackEntryCount() >= 1) {
            for (int i = 0; i < steps; i++) {
                mFMan.popBackStackImmediate(0, 0);
            }
        }
    }

    public void goBackScreen() {
        if (mFMan != null && mFMan.getBackStackEntryCount() >= 1) {
            super.onBackPressed();
        }
    }

    public void goBackScreenOrFinish() {
        if (mFMan != null && mFMan.getBackStackEntryCount() >= 1) {
            super.onBackPressed();
        } else {
            finish();
        }
    }


    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        mPaused = false;
        getCurrentScreen().onActivityResult(requestCode, resultCode, data);

    }

    boolean hasBackstack() {
        return mFMan.getBackStackEntryCount() >= 1;
    }

   
}
